package com.gjs.reviewtree.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gjs.reviewtree.bean.Review;
import com.gjs.reviewtree.controller.vo.AddReviewReqVO;
import com.gjs.reviewtree.service.ReviewService;
import com.gjs.reviewtree.web.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class ReviewController {

    @Resource
    private ReviewService reviewService;

    /**
     * 增加评论
     * @param reqVO
     * @return
     */
    @PostMapping("/reviews")
    public CommonResult<Review> add(@RequestBody @Validated AddReviewReqVO reqVO) {
        return CommonResult.success(reviewService.add(reqVO));
    }

    /**
     * 获取评论列表
     * @param page_num
     * @param page_size
     * @param parent_id
     * @return
     */
    @GetMapping("/reviews")
    public CommonResult<IPage<Review>> list(@RequestParam("page_num") Integer page_num, @RequestParam("page_size") Integer page_size,
                                            @RequestParam(value = "parent_id", required = false) Long parent_id) {
        return CommonResult.success(reviewService.list(parent_id, page_num, page_size));
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/reviews/{id}")
    public CommonResult<Boolean> remove(@PathVariable long id) {
        return CommonResult.success(reviewService.remove(id));
    }
}
